package com.sense.kukoo.biz_library.vm

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.sense.kukoo.biz_library.model.HistoryModel
import com.sense.kukoo.common.mvvm.model.BaseMvvmModel
import com.sense.kukoo.common.mvvm.vm.BaseViewModel
import com.sense.kukoo.common.repo.bean.HistoryItemBean
import com.sense.kukoo.common.repo.bean.LibraryListenBean
import com.sense.kukoo.common.repo.bean.QueryPageBean
import com.sense.kukoo.lib_logger.LoggerManager

class HistoryViewModel :BaseViewModel() {

    private val TAG: String = javaClass.simpleName
    private lateinit var mModel:HistoryModel
    private var mIndex = 2
    private val mPageSize = 10

    val mHistory:MutableLiveData<ArrayList<HistoryItemBean>> = MutableLiveData()
    val enableLoadMore:MutableLiveData<Boolean> = MutableLiveData(true)

    override fun createModel(): BaseMvvmModel {
        mModel = HistoryModel()
        return mModel
    }

    override fun initData() {
        refreshData()
    }

    //获取第一页数据
    fun refreshData(){
        LoggerManager.d("HistoryFragment refreshData()")
        request {
            val result = mModel.listenQueryPage(1, mPageSize)
            if (result.success) {
                val data: QueryPageBean<LibraryListenBean> = result.data!!
                val records:List<LibraryListenBean> = data.records
                //
                mIndex=2
                if(data.last){
                    enableLoadMore.postValue(false)
                }else{
                    enableLoadMore.postValue(true)
                }
                val list: ArrayList<HistoryItemBean> = arrayListOf()
                records.forEach {
                    list.add(HistoryItemBean(item = it))
                }
                mHistory.postValue(list)
            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun getMoreData(){
        request {
            val result = mModel.listenQueryPage(mIndex, mPageSize)
            if (result.success) {
                val data: QueryPageBean<LibraryListenBean> = result.data!!
                val records:List<LibraryListenBean> = data.records
                //
                mIndex++
                if(data.last){
                    enableLoadMore.postValue(false)
                }
                //添加并更新数据
                val value:ArrayList<HistoryItemBean> = mHistory.value!!
                records.forEach {
                    value.add(HistoryItemBean(item = it))
                }
                mHistory.postValue(value)

            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
        }
    }

    fun setDeleteItem(deletes:List<HistoryItemBean>){
        if(deletes.size == 0){
            //退出编辑模式
            //EventBus.getDefault().post(LibraryEditEvent(false))
            return
        }
        toView(ACTION_SHOW_DIALOG)
        request {
            val ids = Array<Long>(deletes.size) {
                return@Array deletes.get(it).item.bookId
            }
            val result = mModel.listenDelete(ids)
            if (result.success) {
                //删除成功
                val value:ArrayList<HistoryItemBean> = (mHistory.value as ArrayList<HistoryItemBean>?)!!
                value.removeAll(deletes)
                mHistory.postValue(value)
                //EventBus.getDefault().post(LibraryEditEvent(false))
            } else {
                Log.e(TAG, "result0=" + result.msg)
                result.msg.let { sendHintMessage(it) }
            }
            toView(ACTION_HIDE_DIALOG)
        }
    }

}